package org.dfl.plugin.editor.parts;

import org.dfl.messages.Messages;
import org.dfl.plugin.editor.model.ConnectionModelElement;
import org.dfl.plugin.editor.model.DflDiagram;
import org.dfl.plugin.editor.model.PlaceModelElement;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;


/**
 * Factory that maps model elements to edit parts.
 * 
 * @author Piotr Wlodarczyk
 */
public class DflEditPartFactory implements EditPartFactory {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart,
	 *      java.lang.Object)
	 */
	public EditPart createEditPart(EditPart context, Object modelElement) {
		// get EditPart for model element
		EditPart part = getPartForElement(modelElement);
		// store model element in EditPart
		part.setModel(modelElement);
		return part;
	}

	/**
	 * Maps an object to an EditPart.
	 * 
	 * @throws RuntimeException
	 *             if no match was found (programming error)
	 */
	private EditPart getPartForElement(Object modelElement) {
		if (modelElement instanceof DflDiagram) {
			return new DiagramEditPart();
		}
		
		if (modelElement instanceof TransitionModelElement) {
			return new TransitionEditPart();
		}
		
		if (modelElement instanceof PlaceModelElement) {
			return new PlaceEditPart();
		}
		
		if (modelElement instanceof ConnectionModelElement) {
			return new ConnectionEditPart();
		}

		throw new RuntimeException(Messages.DflEditPartFactory_0
				+ ((modelElement != null) ? modelElement.getClass().getName()
						: "null")); //$NON-NLS-1$
	}

}